package com.mjf.ue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mjf.ue.domain.U8ProductInEntity;
import com.mjf.ue.domain.U8ProductInItemsEntity;
import com.mjf.ue.domain.U8ProductInListEntity;
import com.mjf.ue.mapper.U8ProductInMapper;
import com.mjf.ue.service.AsyncUEService;
import com.mjf.ue.service.IU8ProductInItemsService;
import com.mjf.ue.service.IU8ProductInListService;
import com.mjf.ue.service.IU8ProductInService;
import com.mjf.ue.vo.U8ProductInListParam;
import com.mjf.ue.vo.U8ProductInListResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2024/12/19 10:28
 */
@Service
public class U8ProductInServiceImpl extends ServiceImpl<U8ProductInMapper, U8ProductInEntity> implements IU8ProductInService {

    @Autowired
    private AsyncUEService asyncUEService;
    @Autowired
    private IU8ProductInListService u8ProductInListService;
    @Autowired
    private IU8ProductInItemsService u8ProductInItemsService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getProductInOrderList(U8ProductInListParam param, boolean flag) {
        U8ProductInListResultVO productInOrderList = asyncUEService.getProductInOrderList(param, flag);
        List<String> codeList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(productInOrderList.getProductinlist())) {
            List<U8ProductInEntity> productInList = new ArrayList<>();
            List<U8ProductInItemsEntity> items = new ArrayList<>();
            List<U8ProductInListEntity> entities = new ArrayList<>();
            for (U8ProductInListResultVO.ProductInfo vo : productInOrderList.getProductinlist()) {
                if (codeList.contains(vo.getCode())) {
                    continue;
                }
                codeList.add(vo.getCode());
                U8ProductInListEntity entity = new U8ProductInListEntity();
                BeanUtil.copyProperties(vo, entity);
                entity.setId(IdUtil.simpleUUID());
                entity.setTimes(LocalDateTime.now());
                // 单据日期
                if (StrUtil.isNotBlank(vo.getDate())) {
                    entity.setDate(LocalDate.parse(vo.getDate()));
                }
                // 审核日期
                if (StrUtil.isNotBlank(vo.getAuditdate())) {
                    entity.setAuditdate(LocalDate.parse(vo.getAuditdate()));
                }
                if (StrUtil.isNotBlank(vo.getDefine5())) {
                    entity.setDefine5(BigDecimal.valueOf(Double.parseDouble(vo.getDefine5())));
                }
                if (StrUtil.isNotBlank(vo.getDefine6())) {
                    entity.setDefine6(LocalDate.parse(vo.getDefine6()));
                }
                if (StrUtil.isNotBlank(vo.getDefine7())) {
                    entity.setDefine7(BigDecimal.valueOf(Double.parseDouble(vo.getDefine7())));
                }
                if (StrUtil.isNotBlank(vo.getDefine15())) {
                    entity.setDefine15(BigDecimal.valueOf(Double.parseDouble(vo.getDefine15())));
                }
                if (StrUtil.isNotBlank(vo.getDefine16())) {
                    entity.setDefine16(BigDecimal.valueOf(Double.parseDouble(vo.getDefine16())));
                }
                entities.add(entity);
                // 详细主表
                if (ObjectUtil.isNotNull(vo.getInfo()) && ObjectUtil.isNotNull(vo.getInfo().getProductin())) {
                    U8ProductInEntity productIn = new U8ProductInEntity();
                    BeanUtil.copyProperties(vo.getInfo().getProductin(), productIn);
                    productIn.setId(IdUtil.simpleUUID());
                    productIn.setTimes(LocalDateTime.now());
                    // 处理特殊类型字段
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDate())) {
                        productIn.setDate(LocalDate.parse(vo.getInfo().getProductin().getDate()));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getArrivedate())) {
                        productIn.setArrivedate(LocalDate.parse(vo.getInfo().getProductin().getArrivedate()));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine4())) {
                        productIn.setDefine4(LocalDate.parse(vo.getInfo().getProductin().getDefine4()));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine5())) {
                        productIn.setDefine5(BigDecimal.valueOf(Double.parseDouble(vo.getInfo().getProductin().getDefine5())));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine6())) {
                        productIn.setDefine6(LocalDate.parse(vo.getInfo().getProductin().getDefine6()));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine7())) {
                        productIn.setDefine7(BigDecimal.valueOf(Double.parseDouble(vo.getInfo().getProductin().getDefine7())));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine15())) {
                        productIn.setDefine15(BigDecimal.valueOf(Double.parseDouble(vo.getInfo().getProductin().getDefine15())));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getDefine16())) {
                        productIn.setDefine16(BigDecimal.valueOf(Double.parseDouble(vo.getInfo().getProductin().getDefine16())));
                    }
                    if (StrUtil.isNotBlank(vo.getInfo().getProductin().getAuditdate())) {
                        productIn.setAuditdate(LocalDate.parse(vo.getInfo().getProductin().getAuditdate()));
                    }
                    productInList.add(productIn);
                    // 详细主表的明细
                    if (CollectionUtil.isNotEmpty(vo.getInfo().getProductin().getEntry())) {
                        for (U8ProductInListResultVO.U8ProductInListInfoResultVO.InfoEntry entry :
                            vo.getInfo().getProductin().getEntry()) {
                            U8ProductInItemsEntity productInItems = new U8ProductInItemsEntity();
                            BeanUtil.copyProperties(entry, productInItems);
                            productInItems.setId(IdUtil.simpleUUID());
                            productInItems.setPInId(productIn.getId());
                            productInItems.setTimes(LocalDateTime.now());
                            // 处理特殊类型字段
                            if (StrUtil.isNotBlank(entry.getQuantity())) {
                                productInItems.setQuantity(BigDecimal.valueOf(Double.parseDouble(entry.getQuantity().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getIrate())) {
                                productInItems.setIrate(BigDecimal.valueOf(Double.parseDouble(entry.getIrate().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getNumber())) {
                                productInItems.setNumber(BigDecimal.valueOf(Double.parseDouble(entry.getNumber().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getPrice())) {
                                productInItems.setPrice(BigDecimal.valueOf(Double.parseDouble(entry.getPrice().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getCost())) {
                                productInItems.setCost(BigDecimal.valueOf(Double.parseDouble(entry.getCost().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine26())) {
                                productInItems.setDefine26(BigDecimal.valueOf(Double.parseDouble(entry.getDefine26().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine27())) {
                                productInItems.setDefine27(BigDecimal.valueOf(Double.parseDouble(entry.getDefine27().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine34())) {
                                productInItems.setDefine34(BigDecimal.valueOf(Double.parseDouble(entry.getDefine34().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine35())) {
                                productInItems.setDefine35(BigDecimal.valueOf(Double.parseDouble(entry.getDefine35().replaceAll(" ", ""))));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine36())) {
                                productInItems.setDefine36(LocalDate.parse(entry.getDefine36().replaceAll(" ", "")));
                            }
                            if (StrUtil.isNotBlank(entry.getDefine37())) {
                                productInItems.setDefine37(LocalDate.parse(entry.getDefine37().replaceAll(" ", "")));
                            }
                            productInItems.setRowno(BigDecimal.valueOf(Double.parseDouble(entry.getRowno())));
                            items.add(productInItems);
                        }
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(productInList)) {
                saveBatch(productInList);
            }
            if (CollectionUtil.isNotEmpty(entities)) {
                u8ProductInListService.saveBatch(entities);
            }
            if (CollectionUtil.isNotEmpty(items)) {
                u8ProductInItemsService.saveBatch(items);
            }
        }
    }
}
